package com.ai.service.impl;

import com.ai.domain.po.ChatMessage;
import com.ai.service.AiRedisService;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.security.Key;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class AiRedisServiceImpl implements AiRedisService {

    private final RedisTemplate<String, Object> redisTemplate;


    private static final String BASIC_KEY = "chat:memory:";
    /**
     * 获取会话记录
     * return conversationId 会话ID
     */
    @Override
    public List<String> GetChatIds(String type) {
        //获取所有的key,获取所有的会话ID
        Set<String> chatKeys = redisTemplate.keys("chat:memory" + ":*");
        return chatKeys.stream()
                .map(key -> key.split(":"))          // 按 ":" 分割成数组
                .filter(parts -> parts.length > 2)   // 确保至少有3部分（如 ["chat", "memory", "xxx"]）
                .map(parts -> parts[2])              // 取第3部分作为 chatId（索引为2）
                .collect(Collectors.toList());
    }

    @Override
    public String GetChatHistory(String chatId, String type) {
        String key = BASIC_KEY + chatId;

        Object ChatMessages = redisTemplate.opsForValue().get(key);

        if (ChatMessages == null) {
            return null;
        }

        return ChatMessages.toString();



    }

}
